<!--
 * @Description:业务系统
-->
<template>
  <div class="registered-container business appMainWarp depart-container">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">
      <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">按天显示</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">按月显示</el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">按年显示</el-button>
        <el-button :type="3 == quaflat ? 'primary':''" @click="getlisttable(3)">汇总显示</el-button>
      </el-form-item>

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        />
      </el-form-item>

      <el-form-item label="院区:">
        <el-select v-model="searchKey.hospital_area_no" placeholder="请选择">
          <el-option
            v-for="item in searchKey.Hospital"
            :key="item.area_code"
            :label="item.area_name"
            :value="item.area_code"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="选择支付场景:">
        <el-select v-model="searchKey.system_no" multiple placeholder="请选择支付场景" clearable>
          <el-option
            v-for="item in searchKey.System"
            :key="item.pay_scene_no"
            :label="item.pay_scene_name"
            :value="item.pay_scene_no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="账单来源:">
        <el-select v-model="searchKey.reconciliation" placeholder="请选择">
          <el-option
            v-for="item in searchKey.reconciliationList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>
    <template>
      <div class="alldata">
        <div class="kioskdata back">
          <div class="histogram">
            <div class="all">
              <pie-chart-group :items-chart="circleData.chart" />
            </div>
            <div class="circle">
              <pie-chart-group :items-chart="alldata.chart" />
            </div>
          </div>
          <div class="hoslist backtable">
            <TableGroup :items-info="searchDate" @devMonito="devData" />
          </div>
        </div>
      </div>
    </template>
    <pagination
      :hidden="page.totalPage<=1"
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 分页
// import BarKioskChart from './components/BarKioskChart'
import PieChartGroup from './components/PieChartGroup'
// import LineChartTwoGroup from './components/LineChartTwoGroup'
import TableGroup from './components/TableGroup'
import { getConfigType, transCount, sectorData } from '@/api/transactRecords/index'

export default {
  name: 'BusinessType',
  components: {
    Pagination,
    PieChartGroup,
    TableGroup
  },
  data() {
    return {
      devData: {
        // title: '医院列表',
        item: null,
        page: {
          tableLoading: false,
          currentPage: 1,
          pageSize: 20,
          totalRow: 0,
          totalPage: 0
        }
      },
      alldata: {
        item: null,
        chart: []
      },
      circleData: {
        item: null,
        chart: []
      },
      kioskdata: {
        item: null,
        chart: []
      },

      quaflat: '0', // 按钮默认
      date: 'd', // 按天月年
      searchKey: {
        time: '',
        hospital_area_no: '', // 选择院区
        Hospital: [],
        system_no: '100', // 选择平台
        System: [],
        reconciliation: '2', // 对账业务
        reconciliationList: [{ name: '支付场景', id: '1' }, { name: 'HIS', id: '2' }, { name: '三方支付', id: '3' }]
      },

      searchDate: [],

      page: {
        tableLoading: false,
        currentPage: 1,
        pageSize: 20,
        totalRow: 0,
        totalPage: 0
      },
      minDate: '',
      maxDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.minDate = minDate
          this.maxDate = maxDate
        },
        disabledDate: (time) => { // 查询时间跨度为365天
          if (this.minDate) {
            var range = 365 * 24 * 3600 * 1000
            return time.getTime() > Date.now() || time.getTime() > (this.minDate.getTime() + range) || time.getTime() < (this.minDate.getTime() - range)
          }
          return time.getTime() > Date.now()
        }
      }
    }
  },
  watch: {
    'searchKey.time': {
      deep: true,
      handler(val) {
        if (val == null) {
          this.minDate = null
        }
      }
    }
  },
  created() {
    this.searchKey.time = this.utils.Foryest()
  },
  mounted() {
    this.getConfig()
    this.onLoad()
    // this.initPieChart()
    // this.initcircleChart()
  },
  methods: {
    sectorDataInit(params) {
      var param = {}
      param.startime = params.starttime
      param.endtime = params.endtime
      param.hospital = params.areano
      param.sceneno = params.sceneno
      param.bustype = params.bustype
      sectorData(param)
        .then(res => {
          this.alldata.item = res.data.trans_arr
          this.circleData.item = res.data.trans_arr
          this.initPieChart(this.alldata)
          this.initcircleChart(this.circleData)
        })
        .catch(() => {})
    },

    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params)
        .then(res => {
          that.searchKey.Hospital = res.data.hospital
          that.searchKey.System = res.data.scene
        })
        .catch(() => {})
    },
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.daytype = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.areano = that.searchKey.hospital_area_no
      params.sceneno = that.searchKey.system_no.toString()
      params.bustype = that.searchKey.reconciliation.toString()
      that.page.tableLoading = true
      this.sectorDataInit(params)
      transCount(params)
        .then(res => {
          that.page.tableLoading = false
          that.searchDate = res.data.result
          that.page.totalPage = res.data.pages
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.daytype = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.starttime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.starttime = ''
        params.endtime = ''
      }
      params.areano = that.searchKey.hospital_area_no
      params.payscene = that.searchKey.system_no.toString()
      params.bustype = that.searchKey.reconciliation.toString()
      var viewUrl = `/cms/transCount?&page=${params.page}&limit=${params.limit}&daytype=${
        params.daytype
      }&areano=${
        params.areano
      }&starttime=${
        params.starttime
      }&endtime=${params.endtime}&bustype=${params.bustype}&export=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },
    getlisttable(flat) {
      this.quaflat = flat
      if (this.quaflat === 0) {
        this.date = 'day'
        this.onLoad()
      } else if (this.quaflat === 1) {
        this.date = 'month'
        this.onLoad()
      } else if (this.quaflat === 2) {
        this.date = 'year'
        this.onLoad()
      } else {
        this.date = 'all'
        this.onLoad()
      }
    },
    initcircleChart(circleData) {
      console.log(circleData, 'pppp')
      this.circleData.chart = [
        {
          color: ['#25D096', '#408AFF'],
          id: 'pieChart1',
          title: '交易类型金额占比',
          series: [
            {
              type: 'pie',
              radius: ['40%', '80%'],
              data: [
                {
                  // value: parseFloat(
                  //   this.registrationData.item.statistics.total_yygh_num
                  // ),
                  value: circleData.item.trade_amount || 0,
                  name: '正交易',
                  label: {
                    normal: {
                      formatter: '{b}\n{d}%',
                      textStyle: {
                        color: '#fff' // 改变标示文字的颜色
                      }
                    }
                  }
                },
                {
                  // value: parseFloat(
                  //   this.registrationData.item.statistics.total_jrgh_num
                  // ),
                  value: circleData.item.negative_amount || 0,
                  name: '负交易',
                  label: {
                    normal: {
                      formatter: '{b}\n{d}%',
                      textStyle: {
                        color: '#fff' // 改变标示文字的颜色
                      }
                    }
                  }
                }
              ],
              label: {
                position: 'inner'
              }
            }
          ]
        }
      ]
    },
    initPieChart() {
      this.alldata.chart = [
        {
          color: ['#CF8FF8', '#3DC4E3'],
          id: 'pieChart2',
          title: '交易类型笔数占比',
          series: [
            {
              type: 'pie',

              data: [
                {
                  // value: parseFloat(
                  //   this.registrationData.item.statistics.total_yygh_num
                  // ),
                  value: this.alldata.item.trade_num || 0,
                  name: '正交易',
                  label: {
                    normal: {
                      formatter: '{b}\n{d}%',
                      textStyle: {
                        color: '#fff' // 改变标示文字的颜色
                      }
                    }
                  }
                },
                {
                  // value: parseFloat(
                  //   this.registrationData.item.statistics.total_jrgh_num
                  // ),
                  value: this.alldata.item.negative_num || 0,
                  name: '负交易',
                  label: {
                    normal: {
                      formatter: '{b}\n{d}%',
                      textStyle: {
                        color: '#fff' // 改变标示文字的颜色
                      }
                    }
                  }
                }
              ],
              label: {
                position: 'inner'
              }
            }
          ]
        }
      ]
    }
  }
}
</script>

<style lang="scss" scoped>
.business {
  background: RGBA(239, 238, 243, 1);

  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
    .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
  .kioskdata {
    margin: 20px 0;
  }
  .back {
    padding: 20px 40px;
    background: #fafafb;
    border-radius: 10px;
  }
  .backtable{
    padding-top: 20px;
  }
  .alldata {
    margin-bottom: 20px;
  }
  .back {
    padding: 20px 40px;
    background: #fafafb;
    border-radius: 10px;
  }
  .histogram {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .all {
      width: 48%;
      //   margin-right: 30px;
      text-align: center;
    }
    .circle {
      width: 48%;
      text-align: center;
    }
  }
}
</style>
